package org.pageley.games.fleetcommand.domain.building;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.HashSet;

import org.pageley.games.fleetcommand.domain.Resource;

public class ProductionBehavior implements Behavior {

	private static final BigDecimal SECONDS_IN_AN_HOUR = new BigDecimal(3600);
	private static final MathContext MATH_CONTEXT = new MathContext(20, RoundingMode.HALF_EVEN);
	private Collection<BehaviorListener> listeners = new HashSet<BehaviorListener>();
	private Resource resource;
	private BigDecimal secondsPerResource;
	private BigDecimal progress = BigDecimal.ZERO;

	public ProductionBehavior(Resource resource, int resourcesPerHour) {
		this.resource = resource;
		this.secondsPerResource = SECONDS_IN_AN_HOUR.divide(new BigDecimal(resourcesPerHour), MATH_CONTEXT);
	}

	@Override
	public void execute(int seconds) {
		progress = progress.add(new BigDecimal(seconds).divide(secondsPerResource, MATH_CONTEXT));
		int produced = progress.intValue();
		progress = progress.subtract(new BigDecimal(produced));
		for(BehaviorListener listener: listeners) {
			listener.produced(resource, produced);
		}
	}

	@Override
	public void addListener(BehaviorListener listener) {
		listeners.add(listener);
	}

	@Override
	public int getResource(Resource resource) {
		return 0;
	}

}
